$(function () {

    //调用初始化菜单方法
    initWholeTree();
    //添加
    $("#menuAddBtn").click(function () {
        var name=$('[name="name"]').val();
        var url=$('[name="url"]').val();
        var icon=$('[name="icon"]').val();

        if (name.length==0){
            layer.msg("名称不能为空");
            return;
        }
        if (url.length==0){
            layer.msg("url不能为空");
            return;
        }

        //发送ajax请求
        $.ajax({
            "url":"/menu/save",
            "type":"post",
            "dataType":"json",
            "data":{
                "name":name,
                "url":url,
                "pid":window.id,   // 当前操作的节点是新节点的父节点
                "icon":icon
            },
            success:function (ajaxResult) {
                if(ajaxResult.result == "SUCCESS") {
                    layer.msg("操作成功！");

                    initWholeTree();
                }
                else{
                    layer.msg('添加失败');
                }
            }
        });

        $("#menuAddModal").modal("hide");

    });
    //修改
    $("#menuEditBtn").click(function () {
        var name=$("#updateName").val();
        var url=$("#updateUrl").val();
        var icon = $("#menuEditModal [name='icon']:checked").val();
        $.ajax({
            url:"/menu/update",
            data:{
                "id":window.menuId,
                "name":name,
                "url":url,
                "icon":icon
            },
            dataType:"json",
            type:"post",
            success:function (ajaxResult) {
                if(ajaxResult.result=="SUCCESS"){
                    layer.msg("修改成功");
                }else {
                    layer.msg("修改失败");
                }
                initWholeTree();
                $("#menuEditModal").modal("hide");
            }
        });
    });

});
//初始化zTree
function initWholeTree() {
    // setting对象中包含zTree的设置属性
    var setting = {
        "view": {
            //设置自定义图标
            "addDiyDom": showMyIcon,
            "addHoverDom": addHoverDom,
            "removeHoverDom": removeHoverDom
        },
        "data": {
            "key": {
                "url": "notExistsProperty" // 阻止点击节点后跳转
            }
        }
    };

    $.ajax({
        "url":"/menu/get/whole/tree",
        "type":"post",
        "dataType":"json",
        "success":function (ajaxResult) {
            if(ajaxResult.result=="SUCCESS"){
                var zNodes=ajaxResult.data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }
        }
    });
}
//自定义图标
function showMyIcon(treeId, treeNode) {
    // console.log(treeId);//ztree控件的ID
    // console.log(treeNode);//一个菜单对象

    //获取treeNode的tId
    var tId=treeNode.tId;//treeDemo_3
    //拼接图标span的ID
    var targetSpanId=tId+"_ico";
    //新的样式表
    var newClass=treeNode.icon;

    //替换新样式
    $("#"+targetSpanId).removeClass().addClass(newClass).css("background","");
}

//鼠标悬浮
function addHoverDom(treeId, treeNode){
    // console.log("鼠标悬浮在"+treeNode.tId);
    // 组装按钮组所在的span标签的id
    var gpbspanid=treeNode.tId+"_btnGrp";
    //判断按钮组是否已经存在
    if($('#'+gpbspanid).length>0){
        return;
    }
    //获取按钮组
    var btnGrp=generateBtnGrp(treeNode);
    // 按id生成规则组装a标签的id
    var a_id=treeNode.tId+"_a";
    //追加到a标签里面
    $("#"+a_id).append(btnGrp);
}

function removeHoverDom(treeId, treeNode){
    // console.log("鼠标移出"+treeNode.tId);

    // 组装按钮组所在的span标签的id
    var gpbspanid=treeNode.tId+"_btnGrp";
    // 删除对应的元素
    $('#'+gpbspanid).remove();
}

//生成按钮组
function generateBtnGrp(treeNode){

    //获取菜单在数据库中的id
    var tagId=treeNode.id;

    //定义三个按钮
    var addBtn = "<a onclick='showAddModal(this)' id='"+tagId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='添加子节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-plus rbg'></i></a>";
    var editBtn = "<a onclick='showEditModal(this)' id='"+tagId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='编辑节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-edit rbg'></i></a>";
    var removeBtn = "<a onclick='showConfirmModal(this)' id='"+tagId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='删除节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-times rbg'></i></a>";

    //按钮组的命名规则 “treeDemo_1_btnGrp”
    var gpbspanid=treeNode.tId+"_btnGrp";
    //定义按钮组span
    var $span=$("<span id='"+gpbspanid+"'></span>");

    // 判断级别
    var level=treeNode.level;
    if (level==0){
        $span.append(addBtn);
    }
    if(level==1){
        if (treeNode.children.length>0){
            $span.append(addBtn);
            $span.append(editBtn);
        }else{
            $span.append(addBtn);
            $span.append(editBtn);
            $span.append(removeBtn);
        }
    }
    if (level==2){
        $span.append(editBtn);
        $span.append(removeBtn);
    }
    if (level==3){
        $span.append(editBtn);
        $span.append(removeBtn);
    }
    return $span;
}
//添加
function showAddModal(addBtn) {
    window.id=addBtn.id;//pid
    $("#menuAddModal").modal("show");
}
//修改
function showEditModal(editBut) {
    $("#menuEditModal").modal("show");
    //数据回显
    window.menuId=editBut.id;//当前节点的id

    //发送ajax请求
    $.ajax({
        "url":"/menu/get/"+window.menuId,
        "type":"get",
        "dataType":"json",
        success:function (ajaxResult) {
            if(ajaxResult.result=="SUCCESS"){
                $('[name="name"]').val(ajaxResult.data.name);
                $('[name="url"]').val(ajaxResult.data.url);
                $('[name="icon"][value="'+ajaxResult.data.icon+'"]').attr("checked",true);
            }
            else{
                layer.msg('显示数据失败');
            }
        }
    });
}

//删除
function showConfirmModal(removeBtn) {
    $("#showConfirmModal").modal("show");
    //数据回显
    window.menuId=removeBtn.id;//当前节点的id
    //发送ajax请求
    $.ajax({
        "url":"/menu/get/"+window.menuId,
        "type":"get",
        "dataType":"json",
        success:function (ajaxResult) {
            if(ajaxResult.result=="SUCCESS"){
                $('[name="name"]').val(ajaxResult.data.name);
                $('[name="url"]').val(ajaxResult.data.url);
                $('[name="icon"][value="'+ajaxResult.data.icon+'"]').attr("checked",true);
            }
            else{
                layer.msg('显示数据失败');
            }
        }
    });
    //删除
    $("#removeBtn").click(function () {
        $.ajax({
            url:"/menu/menuRemove/?id="+window.menuId,
            dataType:"json",
            type:"post",
            success:function (ajaxResult) {
                if (ajaxResult.result=="SUCCESS"){
                    layer.msg("删除成功");
                } else{
                    layer.msg("删除失败");
                }
                initWholeTree();
                $("#showConfirmModal").modal("hide");
            }
        });
    });
}